<template>
  <!-- 模型分享预览页面 -->
  <div
    v-loding="loadingState.isChecking"
    class="bimPreview-container"
  >
    <model-detail
      v-if="visibleState.showModel"
      :model-list="modelList"
    />
    <a-empty
      v-else
      class="empty-wrapper"
      description="分享链接已失效"
    />
  </div>
</template>

<script>
import ModelDetail from '@/components/BimIntegrate/index.vue'
import { validateShareExpire } from '@/api/bimIntegrate/business.js'
import { Empty } from 'ant-design-vue'

export default {
  name: 'BimPreview',
  components: {
    ModelDetail,
    AEmpty: Empty,
  },
  data() {
    return {
      modelList: [],
      visibleState: {
        showModel: false,
      },
      loadingState: {
        isChecking: false
      }
    }
  },
  created() {
    this.initPage()
  },
  methods: {
    /**
     * 验证该次分享链接是否过期
     * @param {String} uuid
     */
    async validateShareExpireReq(uuid) {
      try {
        this.loadingState.isChecking = true
        const params = {
        uuid,
        }
        await validateShareExpire(params)
        this.visibleState.showModel = true
      } catch(e) {
        console.error(e)
      }
      this.loadingState.isChecking = false
    },
    initPage() {
      try {
        // 解码参数
        const url = decodeURI(window.location.href)
        // 获取参数列表
        const searchList = decodeURIComponent(url.split('?')[1]).split('&')
        const queryList = searchList
          .map((item) => {
            const query = item.split('=')
            return {
              name: query[0],
              value: query[1]
            }
          })
        const modelListTarget = queryList.find((item) => item.name === 'modelList')
        const uuidTarget = queryList.find((item) => item.name === 'uuid')
        this.modelList = JSON.parse(modelListTarget.value)
        this.validateShareExpireReq(uuidTarget.value)
      } catch (e) {
        console.error(e)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.bimPreview-container {
  .empty-wrapper {
    padding-top: calc(var(--heightVh) * 0.3);
  }
}
</style>